<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>访问 HTTP(s) 网址</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="wrappers.file.html">« file://</a></li>
      <li style="float: right;"><a href="wrappers.ftp.html">ftp:// »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="wrappers.html">支持的协议和封装协议</a></li>
    <li>访问 HTTP(s) 网址</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="wrappers.http" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">http://</h1>
  <h1 class="refname">https://</h1>
  <p class="refpurpose"><span class="refname">http://</span> -- <span class="refname">https://</span> &mdash; <span class="dc-title">访问 HTTP(s) 网址</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-wrappers.http-description">
  <h3 class="title">说明</h3>
  <p class="para">
   允许通过 HTTP 对文件/资源进行可读访问。默认使用 HTTP 1.0 GET。
   HTTP 请求会附带一个 <code class="literal">Host:</code> 头，用于兼容基于域名的虚拟主机。
   如果在你的 <var class="filename">php.ini</var> 文件中或字节流上下文（context）配置了 <a href="filesystem.configuration.html#ini.user-agent" class="link">user_agent</a> 字符串，它也会被包含在请求之中。
  </p>
  <p class="simpara">
   数据流允许读取资源的 <em class="emphasis">body</em>，而 headers 则储存在了 <var class="varname"><a href="reserved.variables.httpresponseheader.html" class="classname">$http_response_header</a></var> 变量里。
  </p>
  <p class="simpara">
   如果需要知道文档资源来自哪个 URL（经过所有重定向的处理后），
   需要处理数据流返回的系列响应报头（response headers）。
  </p>
  <p class="simpara">
   The <a href="filesystem.configuration.html#ini.from" class="link">from</a> directive will be used for the
   <code class="literal">From:</code> header if set and not overwritten by the
   <a href="context.html" class="xref">上下文（Context）选项和参数</a>.
  </p>
 </div>


 <div class="refsect1 usage" id="refsect1-wrappers.http-usage"> 
  <h3 class="title">用法</h3>
  <ul class="itemizedlist">
   <li class="listitem"><span class="simpara"><var class="filename">http://example.com</var></span></li>
   <li class="listitem"><span class="simpara"><var class="filename">http://example.com/file.php?var1=val1&amp;var2=val2</var></span></li>
   <li class="listitem"><span class="simpara"><var class="filename">http://user:password@example.com</var></span></li>
   <li class="listitem"><span class="simpara"><var class="filename">https://example.com</var></span></li>
   <li class="listitem"><span class="simpara"><var class="filename">https://example.com/file.php?var1=val1&amp;var2=val2</var></span></li>
   <li class="listitem"><span class="simpara"><var class="filename">https://user:password@example.com</var></span></li>
  </ul>
 </div>
 

 <div class="refsect1 options" id="refsect1-wrappers.http-options">
  <h3 class="title">可选项</h3>
  <p class="para">
   <table class="doctable table">
    <caption><strong>封装协议概要</strong></caption>
    
     <thead>
      <tr>
       <th>属性</th>
       <th>支持</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>受 <a href="filesystem.configuration.html#ini.allow-url-fopen" class="link">allow_url_fopen</a> 限制</td>
       <td>Yes</td>
      </tr>

      <tr>
       <td>允许读取</td>
       <td>Yes</td>
      </tr>

      <tr>
       <td>允许写入</td>
       <td>No</td>
      </tr>

      <tr>
       <td>允许添加</td>
       <td>No</td>
      </tr>

      <tr>
       <td>允许同时读和写</td>
       <td>N/A</td>
      </tr>

      <tr>
       <td>支持 <span class="function"><a href="function.stat.html" class="function">stat()</a></span></td>
       <td>No</td>
      </tr>

      <tr>
       <td>支持 <span class="function"><a href="function.unlink.html" class="function">unlink()</a></span></td>
       <td>No</td>
      </tr>

      <tr>
       <td>支持 <span class="function"><a href="function.rename.html" class="function">rename()</a></span></td>
       <td>No</td>
      </tr>

      <tr>
       <td>支持 <span class="function"><a href="function.mkdir.html" class="function">mkdir()</a></span></td>
       <td>No</td>
      </tr>

      <tr>
       <td>支持 <span class="function"><a href="function.rmdir.html" class="function">rmdir()</a></span></td>
       <td>No</td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>
 

 <div class="refsect1 examples" id="refsect1-wrappers.http-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="wrappers.http.example.basic">
   <p><strong>示例 #1 检测重定向后最终的 URL</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$url&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'http://www.example.com/redirecting_page.php'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #0000BB">$url</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'r'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$meta_data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stream_get_meta_data</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br />foreach&nbsp;(</span><span style="color: #0000BB">$meta_data</span><span style="color: #007700">[</span><span style="color: #DD0000">'wrapper_data'</span><span style="color: #007700">]&nbsp;as&nbsp;</span><span style="color: #0000BB">$response</span><span style="color: #007700">)&nbsp;{<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;我们是否被重定向了？&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">strtolower</span><span style="color: #007700">(</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$response</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">))&nbsp;==&nbsp;</span><span style="color: #DD0000">'location:&nbsp;'</span><span style="color: #007700">)&nbsp;{<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;更新我们被重定向后的&nbsp;$url&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$url&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$response</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

 </div>


 <div class="refsect1 notes" id="refsect1-wrappers.http-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <span class="simpara">
    <a href="book.openssl.html" class="link">openssl</a> 扩展启用后才能够支持 HTTPS 协议。
   </span>
  </p></blockquote>
  <p class="simpara">
   HTTP 连接是只读的；还不支持对一个 HTTP 资源进行写数据或者复制文件。
  </p>
  <p class="simpara">
   比如发送 <em class="emphasis">POST</em> 和 <em class="emphasis">PUT</em> 请求，
   可以在 <a href="context.http.html" class="link">HTTP Contexts</a> 的支持下实现。
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-wrappers.http-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><a href="context.http.html" class="xref">HTTP context 选项</a></li>
   <li class="member"><var class="varname"><a href="reserved.variables.httpresponseheader.html" class="classname">$http_response_header</a></var></li>
   <li class="member"><span class="function"><a href="function.stream-get-meta-data.html" class="function" rel="rdfs-seeAlso">stream_get_meta_data()</a> - 从流或文件指针中获取 header/meta 数据</span></li>
  </ul>
 </div>


</div></div></div></body></html>